/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.client.log.impl;

import com.genia.toolbox.web.gwt.basics.client.CssNames;
import com.genia.toolbox.web.gwt.basics.client.message.Messages;
import com.genia.toolbox.web.gwt.basics.client.widget.HyperLinkButton;
import com.genia.toolbox.web.gwt.basics.client.widget.WidgetFactory;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A logger that writes all its message in a <code>Panel</code>.
 */
public class PanelLogger
    extends AbstractLogger
{
  /**
   * the default value for the maximun number of line that will be displayed in
   * the error panel.
   */
  private static final int DEFAULT_NB_LINE = 20;

  /**
   * the maximun number of line that will be displayed in the error panel.
   */
  private int numberOfLine = DEFAULT_NB_LINE;

  /**
   * the number of line currently written in the error panel.
   */
  private transient int nbElement = 0;

  /**
   * the panel that will contains the errors as well as the button to clear.
   */
  private transient final HorizontalPanel mainPanel = new HorizontalPanel();

  /**
   * the panel in which to add error messages.
   */
  private transient final VerticalPanel errorPanel = new VerticalPanel();

  /**
   * the button to clear the error panel.
   */
  private transient final HyperLinkButton clearButton = WidgetFactory.newHyperLinkButton(Messages.CLEAR_MESSAGE, CssNames.ERROR_PANEL_CLEAR_STYLE_NAME);



  /**
   * constructor.
   */
  public PanelLogger()
  {
    mainPanel.add(errorPanel);
    mainPanel.add(clearButton);
    clearButton.setVisible(false);

    clearButton.addClickListener(new ClickListener()
    {

      public void onClick(Widget sender)
      {
        errorPanel.clear();
        clearButton.setVisible(false);
      }

    });

    mainPanel.setStyleName(CssNames.ERROR_PANEL_STYLE_NAME);
    errorPanel.setStyleName(CssNames.ERROR_PANEL_MESSAGES_STYLE_NAME);
  }



  /**
   * abstract method that really log the message. All the tests have already
   * been performed.
   * 
   * @param message
   *          the message to log
   * @param level
   *          the level of the message
   */
  @Override
  public void doLog(final String message, final Level level)
  {
    final Label label = new Label();
    label.setText(message);
    label.setStyleName(CssNames.ERROR_PANEL_LEVEL_STYLE_NAME[level.getPriority()]);
    nbElement++;
    while (nbElement > numberOfLine) {
      errorPanel.remove(numberOfLine - 1);
      nbElement--;
    }
    errorPanel.insert(label, 0);
    clearButton.setVisible(true);
  }



  /**
   * returns the {@link Panel} that will contain the errors.
   * 
   * @return the errorPanel
   */
  public Panel getErrorPanel()
  {
    return mainPanel;
  }



  /**
   * getter for the numberOfLine property.
   * 
   * @return the numberOfLine
   */
  public int getNumberOfLine()
  {
    return numberOfLine;
  }



  /**
   * setter for the numberOfLine property.
   * 
   * @param numberOfLine
   *          the numberOfLine to set
   */
  public void setNumberOfLine(int numberOfLine)
  {
    this.numberOfLine = numberOfLine;
  }

}
